﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Script.Services;
using System.Web.Services;

namespace NLUNZ
{
    /// <summary>
    /// Summary description for TrackingService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class TrackingService : System.Web.Services.WebService
    {
        //private static string webServiceUrl =  ConfigurationManager.AppSettings["trackingApiUrl"];

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string FindTracking(string code, string trackingNumber)
        {
            //string apiKey = ConfigurationManager.AppSettings["TrackingApiKey"];
            //string url = string.Format(webServiceUrl, apiKey, code, trackingNumber);
            string url =
                string.Format("http://nlucargo.com/GetTrackingData.aspx?code={0}&trackingNumber={1}",code,trackingNumber);
            HttpWebResponse response;
            try
            {
                WebRequest request = WebRequest.Create(url);
                response = (HttpWebResponse)request.GetResponse();
            }
            catch (Exception ex)
            {
                return ex.Message + ex.StackTrace;
            }
            Stream stream = response.GetResponseStream();

            StreamReader reader = new StreamReader(stream);
            //string json = reader.ReadToEnd();
            return reader.ReadToEnd();
        }

        [WebMethod(EnableSession = true)]
        [ScriptMethod(UseHttpGet = false,ResponseFormat = ResponseFormat.Json)]
        public string TESTJSON()
        {
            Employee[] employees = new Employee[2];
            employees[0] = new Employee() { Address = "26 vanbrugh place, bucklands beach", Company = "Datacom", Name = "Steven Wu" };
            employees[1] = new Employee() { Address = "94 tuamata road, mt albert", Company = "Intergen", Name = "Yngchang Wu" };
            return new JavaScriptSerializer().Serialize(employees);
        }
    }

    public class Employee
    {
        public string Name { get; set; }
        public string Company { get; set; }
        public string Address { get; set; }
    }
}
